import { createRouter, createWebHistory } from "vue-router";

import Home from '../components/home/Home.vue'
	import Main from '../components/home/pages/Main.vue'
	import Detail from '../components/home/pages/Detail.vue'
	import Cart from '../components/home/pages/Cart.vue'
	import Order from '../components/home/pages/Order.vue'
	import Menu from '../components/home/pages/Menu.vue'
	import Paymenu from '../components/home/pages/Paymenu.vue'
	import Message from '../components/home/pages/Message.vue'
	
import Admin from '../components/admin/Admin.vue'
	import Bookscon from '../components/admin/pages/Bookscon.vue'
	import Hotcon from '../components/admin/pages/Hotcon.vue'
	import Typecon from '../components/admin/pages/Typecon.vue'
	import Hotconadd from '../components/admin/pages/Hotconadd.vue'
	import Bookup from '../components/admin/pages/Bookup.vue'

import Login from '../components/register/Login.vue'
import Adm from '../components/register/Adm.vue'
import Hom from '../components/register/Hom.vue'
const routes = [
	{
		path:'/',
		redirect:{
			name:'Home'
		}
	},
	{
		path:'/home',
		name:'Home',
		component:Home,
		redirect:{
			name:'Main'
		},
		children:[
			{
				path:'',
				name:'Main',
				component:Main
			},
			{
				path:'detail',
				name:'Detail',
				component:Detail
			},
			{
				path:'cart',
				name:'Cart',
				component:Cart,
				meta:{
					role:'P'
				}
			},
			{
				path:'order',
				name:'Order',
				component:Order,
				meta:{
					role:'P'
				}
			},
			{
				path:'menu',
				name:'Menu',
				component:Menu
			},
			{
				path:'paymenu',
				name:'Paymenu',
				component:Paymenu
			},
			{
				path:'message',
				name:'Message',
				component:Message,
				meta:{
					role:'P'
				}
			}
		]
	},
	{
		path:'/admin',
		name:'Admin',
		component:Admin,
		meta:{
			role:'G'
		},
		children:[
			{
				path:'typecon',
				name:'Typecon',
				component:Typecon
			},
			{
				path:'bookscon',
				name:'Bookscon',
				component:Bookscon
			},
			{
				path:'hotcon',
				name:'Hotcon',
				component:Hotcon
			},
			{
				path:'hotconadd',
				name:'Hotconadd',
				component:Hotconadd
			},
			{
				path:'bookup',
				name:'Bookup',
				component:Bookup
			},
		]
	},
	{
		path:'/login',
		name:'Login',
		component:Login
	},
	{
		path:'/adm',
		name:'Adm',
		component:Adm,
		meta:{
			role:'G'
		}
	},
	{
		path:'/hom',
		name:'Hom',
		component:Hom,
		meta:{
			role:'P'
		}
	},
  ];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

//路由守卫 在跳转下一个页面之前，判断当前用户是否合法/是否具有访问权限
router.beforeEach((to,from,next)=>{
	let role=localStorage.getItem('role')
	
	//进入后台首页 role是否与规则中的要求匹配
	//判断即将进入的路由规则中限制的访问角色规则，是否与当前登录者的角色匹配
	
	if(to.meta.role==undefined || to.meta.role==''){
		next()
		return
	}
	
	if(to.meta.role==role){
		//继续进入
		next()
		return
	}
	//以上都不符合的路由
	router.replace('/').catch((error)=>{
	})
})
export default router;
